package com.druid.common.aspect;

import com.druid.config.DataSourceConfig.DbContextHolder;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
*
* 多数据源动态切换
*
* @author druid-elf
* @since 2021-04-07T20:35:15.418530100
*/
@Component
@Order(value = -100)
@Log4j2
@Aspect
public class DataSourceSwitchAspect {

    // management数据源
    private static final String managementMapper = "com.druid.mapper.managementmapper";
    private static final String managementService = "com.druid.service.managementservice";
    private static final String managementExecution = "execution(* "+managementMapper+"..*.*(..))||execution(* "+managementService+"..*.*(..))";

    @Before(managementExecution)
    public void management() { common("management"); }

    private void common ( String name ) {
        log.info("切换到数据源..."+ name );
        DbContextHolder.setDbType( name );
    }

}
